<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('city_id')->index()->comment('城市ID');
            $table->bigInteger('company_id')->index()->comment('公司ID');
            $table->bigInteger('driver_id')->index()->comment('司机ID');
            $table->bigInteger('user_id')->index()->comment('乘客ID');
            $table->bigInteger('service_type_id')->default(0)->comment('业务类型ID');
            $table->bigInteger('car_id')->comment('车辆ID');
            $table->bigInteger('service_id')->default(0)->comment('服务ID');
            $table->text('call_service_type_id')->comment('呼叫类型ID 逗号隔开');
            $table->char('order_sn', 18)->comment('订单编号');
            $table->decimal('predict_price',10,2)->default(0)->comment('预计金额');
            $table->decimal('should_price',10,2)->default(0)->comment('应收金额');
            $table->integer('user_coupon_id', false, true)->nullable()->default(0)->comment('用户优惠券ID');
            $table->decimal('coupon_price',10,2)->nullable()->default(0)->comment('优惠金额');
            $table->decimal('real_price',10,2)->default(0)->comment('实收金额');
            $table->decimal('real_should_price',10,2)->nullable()->comment('实际应收金额');
            $table->tinyInteger('is_pay_predict', false, true)->default(0)->comment('是否已支付预计金额 0-否 1-是');
            $table->enum('source', [1])->default(1)->comment('订单来源：1-小程序');
            $table->string('invoice_number', 30)->nullable()->comment('发票号');
            $table->enum('status', [0, 1, 2, 3, 4, 5, 6, -1, -2, -3])->default(1)->comment('订单状态：0-待支付预付款，1预约中，2已接单，3正在服务，4未支付，5已支付，6已评价，-1乘客取消，-2司机取消，-3后台取消');
            $table->string('remark')->nullable()->comment('备注');
            $table->timestamp('end_time')->nullable()->comment('结束时间');
            $table->decimal('km', 10, 2)->default(0)->comment('总里程');
            $table->integer('minute')->default(0)->comment('总服务时间');
            $table->decimal('driver_money',10,2)->default(0)->comment('司机应得');
            $table->decimal('surplus_profit', 10, 2)->default(0)->comment('剩余利润');
            $table->string('cancel_reason')->nullable()->comment('撤单理由');
            $table->decimal('cancel_price', 10, 2)->nullable()->comment('取消责任费');
            $table->integer('cancel_time', false, true)->nullable()->comment('取消时间 时间戳');
            $table->tinyInteger('cancel_status', false, true)->nullable()->comment('取消状态 0-取消流程完成 1-需支付取消费用 2-有待退款金额 3-退款中');
            $table->integer('receive_time', false, true)->nullable()->comment('接单时间 时间戳');
            $table->decimal('toll_price', 10, 2)->nullable()->comment('过路费');
            $table->decimal('stop_price', 10, 2)->nullable()->comment('停车费');
            $table->decimal('waiting_time_price', 10, 2)->nullable()->comment('超时等待费');

            $table->index([
                'company_id',
                'driver_id',
                'user_id',
            ]);

            $table->timestamps();
            $table->softDeletes();
        });
        DB::statement("ALTER TABLE `orders` comment '订单表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}
